package controller;

import com.smarthouse.model.ApiResponse;
import com.smarthouse.model.LoginRequest;
import com.smarthouse.model.LoginResponse;
import com.smarthouse.service.UserAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "登录接口")
@RestController
@RequestMapping("/auth")
public class LoginController {

    @Autowired
    private UserAccountService userAccountService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ApiResponse<LoginResponse> login(@RequestBody LoginRequest request) {
        try {
            String token = userAccountService.login(request.getUsername(), request.getPassword());
            return ApiResponse.success(new LoginResponse(token, "登录成功"));
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
} 